// pages/address/address.js
import { request } from "../../utils/index.js";
import { areaList} from "../../utils/areaList.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaList:{},  // 选中省市区数据
    show: false,
    id:"", // 地址id
    name:"", // 姓名
    mobile:"", // 手机号
    province_id:"", // 省id 
    city_id:"", // 市id 
    district_id:"", // 区id
    province:"", // 省
    city:"", // 市
    district:"", // 区
    detail:'',// 详情
    is_default:1, // 是否设为默认:1=是,0=否
    type:0, // 0:创建 1:修改
  },

   // 输入姓名
   onChangeName(event){
    this.setData({name:event.detail})
  },

    // 输入专业
  onChangePhone(event){
    this.setData({mobile:event.detail})
  },
  // 输入详情
  onChangeDetail(event) {
    this.setData({detail:event.detail})
  },

  // 弹窗
  showPopup(event) {
    console.log(event)
    this.setData({show: true});
  },
  // 关闭
  onClose() {
    this.setData({ show: false });
  },

  // 点击确定
  onConfirm(event) {
    this.setData({ show: false });
    var that = this;
    if (event.detail.values.length > 0) {
      for (let i = 0; i < event.detail.values.length; i++) {
        if(i == 0 ){
          var province_id = event.detail.values[0].code;
          var province = event.detail.values[0].name;
          that.setData({
            province_id:province_id,
            province:province,
          })
        }else if (i == 1) {
          var city_id = event.detail.values[1].code;
          var city = event.detail.values[1].name;
          that.setData({
            city_id:city_id,
            city:city
          })
        }else {
          var district_id = event.detail.values[2].code;
          var district = event.detail.values[2].name;
          that.setData({
            district_id:district_id,
            district:district
          })
        }
      }
    }
  },
  // 保存
  onSubmit(){

    console.log(this.data.district_id)
    var that = this;
    if (that.data.type == 0) { // 新增地址
      request({ url: "/api/address/add",data:
      {
        name:that.data.name,
        mobile:that.data.mobile,
        province_id:that.data.province_id,
        city_id:that.data.city_id,
        district_id:that.data.district_id,
        detail:that.data.detail,
        is_default:that.data.is_default,
      } 
      })
      .then(result => {
        wx.showToast({
         title: result.msg,
         icon: 'none',
         duration:2000
        });
      })
    }else {  // 修改地址
      request({ url: "/api/address/edit",data:
      {
        id:that.data.id,
        name:that.data.name,
        mobile:that.data.mobile,
        province_id:that.data.province_id,
        city_id:that.data.city_id,
        district_id:that.data.district_id,
        detail:that.data.detail,
        is_default:that.data.is_default,
      } 
      })
      .then(result => {
        wx.showToast({
         title: result.msg,
         icon: 'none',
         duration:2000
        });
      })
    }
   
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      type:options.type,
      areaList:areaList
    })

    if (options.type == 0) {
      wx.setNavigationBarTitle({
        title: '新建地址',
      })
    }else {
      wx.setNavigationBarTitle({
        title: '修改地址',
      })
      if (options.jsonStr.length > 0) {
        let item=JSON.parse(options.jsonStr);
        console.log(item)
        this.setData({id:item.id});
        this.setData({name:item.name});
        this.setData({mobile:item.mobile});
        this.setData({province_id:item.province_id});
        this.setData({province:item.province_id_text});
        this.setData({city_id:item.city_id});
        this.setData({city:item.city_id_text});
        this.setData({district_id:item.district_id});
        this.setData({district:item.district_id_text});
        this.setData({detail:item.detail});
        this.setData({is_default:item.is_default});
      }
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})